package com.jf.cloud.service.sysmanager.handler;

import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.jf.cloud.service.sysmanager.constant.sms.SMSGNErrorCode;
import com.jf.cloud.service.sysmanager.constant.sms.SmsResult;
import com.jf.cloud.service.sysmanager.entity.sms.BaseSmsApiConfig;
import com.jf.cloud.service.sysmanager.entity.sms.SmsMessage;
import com.jf.cloud.service.sysmanager.support.connect.HttpClientHandle;

/**
 * 嘎牛 发送短信
 * 
 * @author 阿拉丁加布 创建时间：2017年9月19日 下午6:25:42
 */
public class GaniuSmsHandler {
	private static Log log = LogFactory.getLog(GaniuSmsHandler.class);

	public static SmsResult sendSMS(SmsMessage message, BaseSmsApiConfig config) {
		SmsResult smsResult = new SmsResult();
		try {
			HttpClientHandle httpClientHandle = new HttpClientHandle();
			PostMethod postMethod = httpClientHandle.getPostMethod(config.getSmsSendUrl());
			postMethod.setParameter("action", "send");
			postMethod.setParameter("account", config.getSmsAccount());
			postMethod.setParameter("password", config.getSmsAccountPwd());
			postMethod.setParameter("mobile", message.getPhoneNos());
			postMethod.setParameter("content", message.getMessageContent());
			postMethod.setParameter("extno", config.getSmsAccountKey());
			
			String resultStr =  httpClientHandle.doPostURL(postMethod);
			log.info(" 手机号码【" + message.getPhoneNos() + "】，短信内容【" + message.getMessageContent() + "】。");
			log.info(" 嘎牛  短信执行结果 == " + resultStr);
			Document document = DocumentHelper.parseText(resultStr); 
			Element rootElement = document.getRootElement();
			if (rootElement == null) {
				smsResult.setCode("-1");
				return smsResult;
			}
			
			Element returnstatusElement = rootElement.element("returnstatus");
			if (returnstatusElement == null) {
				smsResult.setCode("-1");
				return smsResult;
			}
			
			String returnstatusText = returnstatusElement.getTextTrim();
			if (returnstatusText.equalsIgnoreCase("Success")) {
				Element taskIDElement = rootElement.element("taskID");
				if (taskIDElement != null) {
					String taskIDText = taskIDElement.getTextTrim();
					smsResult.setMsgId(taskIDText);
				} else {
					log.error("发送即时短信失败 taskIDText return null ！返回值【" + resultStr + "】");
				}
				Element messageElement = rootElement.element("message");
				if (messageElement != null) {
					String messageText = messageElement.getTextTrim();
					if (messageText.equalsIgnoreCase("OK")) {
						smsResult.setCode("0");
					} else {
						log.error("发送即时短信失败！返回值【" + resultStr + "】");
						smsResult.setCode(messageText);
						smsResult.setInfo(SMSGNErrorCode.getErrorCodeDesc(messageText));
					}
				}
			} else if (returnstatusText.equalsIgnoreCase("Failed")) {
				Element taskIDElement = rootElement.element("taskID");
				if (taskIDElement != null) {
					String taskIDText = taskIDElement.getTextTrim();
					smsResult.setMsgId(taskIDText);
				} else {
					log.error("发送即时短信失败 taskIDText return null ！返回值【" + resultStr + "】");
				}
				Element messageElement = rootElement.element("message");
				if (messageElement != null) {
					String messageText = messageElement.getTextTrim();
					if (messageText.equalsIgnoreCase("OK")) {
						smsResult.setCode("0");
					} else {
						log.error("发送即时短信失败！返回值【" + resultStr + "】");
						smsResult.setCode(messageText);
						smsResult.setInfo(SMSGNErrorCode.getErrorCodeDesc(messageText));
					}
				}
			} else {
				smsResult.setCode("-1");
			}

		} catch (Exception e) {
			log.error("发送即时短信失败！", e);
			smsResult.setCode("-1");
			smsResult.setInfo("发送即时短信失败" + e.getMessage());
		}
		return smsResult;
	}
	
	public static void main(String[] args) {
		SmsMessage message = new SmsMessage();
		message.setPhoneNos("13572186882");
		message.setMessageContent("123");
		
		BaseSmsApiConfig config = new BaseSmsApiConfig();
		config.setSmsSendUrl("http://smgw.guangtongcloud.com:7862/sms");
		
		GaniuSmsHandler.sendSMS(message, config);
	}
}
